package utils;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JdbcUtils {
    private static DataSource dataSource=null;

    //路径
    static{
     /*   String url = "jdbc:mysql://localhost:3306/by_jdbc?useUnicode=true&charcterEncoding=utf-8&userSSL=false&serverTimezone=Asia/Shanghai";
        String username = "root";
        String pwd = "123456";*/
        try(/*FileInputStream fis=new FileInputStream("JdbcUtils.properties")*/
                InputStream is=JdbcUtils.class.getResourceAsStream("/JdbcUtils.properties")

        ){
            Properties p=new Properties();
            p.load(is);
            Class.forName(p.getProperty("driver"))  ;
            dataSource = new DriverManagerDataSource(p.getProperty("url"), p.getProperty("username"), p.getProperty("pwd"));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        //创建连接池

    }
    public static JdbcTemplate getJdbcTemplate() {

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    return jdbcTemplate;
    }
}
